package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfWalletInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.xfwalletinfo.SetConsAllWalletRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoWalletInfo;
import com.ocom.payconfig.service.XfWalletInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class XfWalletInfoServiceImpl extends ServiceImpl<IDaoWalletInfo, EntityXfWalletInfo> implements XfWalletInfoService {

    @Autowired
    private IDaoWalletInfo walletInfo;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfWalletInfo> findByCompanyIdAndState(Long companyId, Integer state) {
        QueryWrapper<EntityXfWalletInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        if (null != state) {
            queryWrapper.eq("state", state);
        }
        queryWrapper.orderByDesc("wallet_no");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Result setConsWallet(Long companyId, SetConsAllWalletRequest request) {
        QueryWrapper<EntityXfWalletInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", request.getComId());
        queryWrapper.eq("wallet_name", request.getWalletName());
        queryWrapper.ne("wallet_no", request.getWalletNo());
        queryWrapper.last("limit 1");
        EntityXfWalletInfo getOneEntity = this.getOne(queryWrapper);
        if (null != getOneEntity) {
            return ResultUtil.error(ResultEnum.Err_4121);
        }
        queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", request.getComId());
        queryWrapper.eq("wallet_no", request.getWalletNo());
        queryWrapper.last("limit 1");
        getOneEntity = this.getOne(queryWrapper);

        if (null == getOneEntity) {
            getOneEntity = new EntityXfWalletInfo();
        }

        BeanUtils.copyProperties(request, getOneEntity);

        return ResultUtil.success(this.saveOrUpdate(getOneEntity));
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public void saveXfWalletInfo(Long companyId, EntityXfWalletInfo entityXfWalletInfo) {
        this.save(entityXfWalletInfo);
    }
}
